package com.ch3cooh.proxy.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ch3cooh.proxy.pojo.ClassInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zxl
 * @Date 2018/4/10
 * @Description: OkHttpClient的工具类
 */
public class OkHttpClientUtil {

    public static final String permissionToken = "1a2b3c4d5e";

    public static Response post(String url, Map<String, String> parmMap) {
        Response response = null;
        try {
            OkHttpClient okHttpClient = new OkHttpClient();
            FormBody.Builder fb = new FormBody.Builder();
            if (parmMap != null) {
                for (Map.Entry<String, String> entry : parmMap.entrySet()) {
                    String name = entry.getKey();
                    String value = entry.getValue();
                    fb.add(name, value);
                }
            }
            RequestBody body = fb.build();
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            Call call = okHttpClient.newCall(request);
            response = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 发送classInfo 以实现 rpc调用
     * @param url
     * @param classInfo
     * @return
     */
    public static Object postClassInfo(String url , ClassInfo classInfo , Class methodReturnType){
        String jsonString = JSON.toJSONString(classInfo);
        HashMap<String,String> params  = new HashMap();
        params.put("jsonString", jsonString);
        String result = postString(url, params);
        if(String.class.equals( methodReturnType ) ) return result;
        return  JSONObject.parseObject(result, methodReturnType);
    }

//    public static void main(String[] args) {
//        JSONObject.parse();
//    }

    /**
     * http调用，直接返回String
     * @param url
     * @param parmMap
     * @return
     */
    public static String postString(String url, Map<String, String> parmMap) {
        Response post = post(url, parmMap);
        try {
            return post.body().string();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }



    public static Response postJson(String url, String json) {
        Response response = null;
        try {
            OkHttpClient okHttpClient = new OkHttpClient();
            /**
             * 创建请求的参数body
             */
            RequestBody body = FormBody.create(MediaType.parse("application/json"), json);
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            Call call = okHttpClient.newCall(request);
            response = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }
}
